local Config = require("Config")
local Util = require("common.Util")
local Log = require("common.Log")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Util = require("common.Util")
local InnerMsg = require("core.InnerMsg")
local CommonDB = require("common.CommonDB")
local JjcDB = require("jjc.JjcDB")
local VipLogic = require("vip.VipLogic")	
local VipExcel = require("excel.vip")
local GMExcel = require("excel.gm")
local RoleLogic = require("role.RoleLogic")
local UnionDBLogic = require("union.UnionDBLogic")
local CombatPosLogic = require("combat.CombatPosLogic")
local CombatDefine = require("combat.CombatDefine")
local ItemDefine = require("bag.ItemDefine")
local MiddleConnect = require("middle.MiddleConnect")

d3 = {}

local function send(human, err)
	local mm = Msg.gc.GC_CHAT
	mm.item.msgType = 3
	mm.item.targetUuid = ""
	mm.item.sendTime = os.time()
	mm.item.msg = tostring(err)
    mm.item.vipLv = human.db.vipLv or 0
    mm.item.videoUuid = ""
    mm.item.isJson = 0
    mm.item.unionFrame = 0
    local union = {}
    if human.db.unionUuid ~= nil then
        union = UnionDBLogic.getUnion(human.db.unionUuid)
    end
	RoleLogic.getRoleBase(human,mm.item.roleBase)
    mm.item.label = human.db.throneLabel or 0
	Msg.send(mm, human.fd)
end

function checkGm(human, str)
	if not Config.IS_USE_GM_CMD and human.db.gm == nil then
		return
	end
	local pos = string.find(str, " ", 1, true)
	local cmd = string.sub(str, 1, pos and pos - 1)	
	
	if not Config.IS_USE_GM_CMD then
		if human.db.gm == 1 and not GmControl[cmd] then -- 一级权限的gm只能使用部分gm命令
			send(human, "not permit for this gm cmd!")
			return true
		end
		if human.db.gm == 100 then -- 新手引导员不能使用gm命令
			return
		end
	end

	if not d3[cmd] then
		return
	end
	local arg = string.sub(str, #cmd + 2)
	Log.write(Log.LOGID_OSS_GM_CODE, human.db._id, human.db.account, human.db.name, cmd, arg)
	
	d3[cmd](human, tonumber(arg) or arg)
	return true
end

function d3.heroadd(human,val)
	if val == nil then return end
	local tb = Util.split(val, " ")
	
	local id = tonumber(tb[1])
	local cnt = tonumber(tb[2])
	local lv = tonumber(tb[3])
    local star = tonumber(tb[4])
    if not (id and cnt) then return end

    for i = 1, cnt do
        local heroGrid = require("hero.HeroGrid").createHeroGrid(id , star)
        if not heroGrid then break end
        
        local attrConfig = require("hero.HeroDefine").getAttrConfig(id, heroGrid.star)
        if not attrConfig then return end
        local quality = require("hero.HeroGrid").getMaxQualityByLv(heroGrid.star, lv)
        local maxLv = require("hero.HeroGrid").getMaxLv(heroGrid.star, quality)

        heroGrid.lv = math.min(maxLv,  lv or 0)
        heroGrid.quality = math.max(heroGrid.quality, quality or 0)

        require("hero.HeroLogic").addHeroByGrid(human, heroGrid, "gm")
    end
end

function d3.herodel(human,val)
	if val == nil then return end
	local pos = string.find(val, " ", 1, true)
	if pos == nil then return end
	
	local index = tonumber(string.sub(val, 1, pos - 1))
	local cnt= tonumber(string.sub(val,pos+1,#val))
	require("hero.HeroLogic").delHeroByIndex(human,index,"gm")
end

function d3.herodelAll(human,val)
    for index = 1, human.db.heroBag[0]do
       require("hero.HeroLogic").delHeroByIndex(human,index,"gm")
    end
end

function d3.itemadd(human,val)
	if val == nil then return end
	local pos = string.find(val, " ", 1, true)
	if pos == nil then return end

	local id = tonumber(string.sub(val, 1, pos - 1))

    if not id then
        local name = string.sub(val, 1, pos - 1)
        local ItemExcel = require("excel.item").item
        for k,v in pairs(ItemExcel) do
            if v.name == name then
                id = k
                break
            end
        end
    end
    if not id then
        local name = string.sub(val,1,pos - 1)
        local EquipExcel = require("excel.equip").equip
        for k,v in pairs(EquipExcel) do
            if v.name == name then
                id = k
                break
            end
        end
    end
    if not id then
        return
    end
	local EquipExcel = require("excel.equip").equip	
	if EquipExcel[id] then
		local cnt= tonumber(string.sub(val,pos+1,pos + 2))
		local rare= tonumber(string.sub(val,pos+2,#val))
		require("bag.BagLogic").addItem(human, id, cnt, "gm",  nil, rare)  
		human.getEquip = nil
	else
		-- 分开是因为 常规道具 给不了
		local cnt= tonumber(string.sub(val,pos+1,#val))
		require("bag.BagLogic").addItem(human,id,cnt,"gm")
	end
	
end

-- 删除道具
function d3.itemdel(human,val)
	if val == nil then return end
	local pos = string.find(val, " ", 1, true)
	if pos == nil then return end
	
	local id = tonumber(string.sub(val, 1, pos - 1))
	local cnt= tonumber(string.sub(val,pos+1,#val))
	require("bag.BagLogic").delItem(human,id,cnt,"gm")
end

function d3.qingbaoMax(human)

	require("bar.BarTaskLogic").addBarQingBao(human, 10000)
end

function d3.maxbattle(human, val)
	val = val + 0
	
	human.db.battleID = val
	human.db.guajiID = val - 1

	local BRoleLogic = require("billboard.BRoleLogic")
	local BillboardDefine = require("billboard.BillboardDefine")
	BRoleLogic.updateData(BillboardDefine.TYPE_BATTLE, human.db)	
end

function d3.dbsave(human)
	ObjHuman.save(human)
end

-- 设置当前金币
function d3.jinbi(human, val)
	val = val + 0	
	ObjHuman.updateJinbi(human, val - human.db.jinbi, "gm")
end

-- 设置钻石
function d3.zuanshi(human, val)
	val = val + 0
	
	if val > human.db.zuanshi then
		ObjHuman.addZuanshi(human, val - human.db.zuanshi, "gm")
	else 
		ObjHuman.decZuanshi(human, val - human.db.zuanshi, "gm")
	end
end

--添加邮件
function d3.mail(human,val)
	local param = Util.split(val, " ")
	local mailtype = tonumber(param[1])
	local title = param[2]
	local content = param[3]
	local max = #param
    local count = (type(param[max]) == "number") and param[max] or 1
	local items
	if param[4] then
		items = {}
		for i = 4,max-1 do
			items[#items + 1] = {tonumber(param[i]),math.floor(math.random(1,10))}
		end
	end
	if not (mailtype == 1 or mailtype == 2) or not title or not content then
		send(human, "参数错误 "..type.." "..title.." "..content.." "..#items)
	end
	local MailManager = require("mail.MailManager")
	for i= 1,count do 
	    MailManager.add(mailtype,human.db._id,title,content,items,mailtype == 2 and [[测试用户]]) 
    end
end

function d3.clbag(human)
	human.db.bag = {}
	local msgRet = Msg.gc.GC_BAG_LIST
	msgRet.list[0] = 0 
	Msg.send(msgRet, human.fd)
end

function d3.clhero(human)
	local HeroLogic = require("hero.HeroLogic")
	for i = 1, human.db.heroBag[0] do
		HeroLogic.delHeroByIndex(human, i, "gm")
	end
end

function d3.god(human)

	local GMExcelConfig = GMExcel.god[1]
	BagLogic.addItemList(human, GMExcelConfig.addItem, "gm")

    for k, v in ipairs(GMExcelConfig.addHero) do
        local id = tonumber(v[1])
	    local cnt = tonumber(v[2])
	    local lv = tonumber(v[3])
        local star = tonumber(v[4])
		local quality = require("hero.HeroGrid").getMaxQualityByLv(star, lv)
		for i = 1, cnt do 
			local heroGrid = require("hero.HeroGrid").createHeroGrid(id, star)
			if heroGrid then 
			   heroGrid.lv = math.max(heroGrid.lv, lv or 0)
			   heroGrid.quality = math.max(heroGrid.quality, quality or 0)
			   require("hero.HeroLogic").addHeroByGrid(human, heroGrid, "gm")
			end
		end      
    end
	
	--d3.viplv(human, 13)
	--d3.lv(human, 1)
	--d3.noguide(human)
end

function d3.wudi(human)
	if wudi then
		wudi = false
		Broadcast.sendErr(human, "取消无敌状态")
	else
		wudi = true
		Broadcast.sendErr(human, "进入无敌状态")
	end
end

-- 清空副本次数和购买次数
function d3.clcopy(human)
	human.db.copy = nil
end

-- 设置等级
function d3.lv(human, val)
	val = val + 0
	local lvOld = human.db.lv
	local lvNew = val + 0
	human.db.lv = lvNew
	
    ObjHuman.onLvUpCB(human,lvOld , lvNew)
end

-- 加经验
function d3.addexp(human, val)
	val = val + 0
	ObjHuman.addExp(human, val)
end

-- 设置vip等级
function d3.viplv(human, val)
	val = val + 0
	
	local oldVipLv = VipLogic.getVipLv(human)
	
	human.db.vipExp = VipExcel.vip[val] and VipExcel.vip[val].exp or 0
	human.db.vipLv = val
	VipLogic.addExp(human, 0)
end

-- 增加vip经验
function d3.vipexp(human, val)	
	val = val + 0

	VipLogic.addExp(human, val)	
end

function d3.it1(human, value)
	value = tonumber(value)
	if not value then return end

	local ItemExcel = require("excel.item").item	
	local BagLogic = require("bag.BagLogic")
	for itemID, cf in pairs(ItemExcel) do
		if cf.subType == value then
			BagLogic.addItem(human, itemID, 1, "gm", true)
		end
	end
	BagLogic.sendBagList(human)
end

function d3.equip(human, value)
	value = tonumber(value)
	if not value then return end

	local BagLogic = require("bag.BagLogic")
	local EquipExcel = require("excel.equip").equip
	for itemID in pairs(EquipExcel) do
		BagLogic.addItem(human, itemID, 1, "gm", true)
	end	
	BagLogic.sendBagList(human)
end

function d3.task(human, val)
	if val == nil then return end
	local task = string.find(val, " ", 1, true)
	if task == nil then return end
	
	local id = tonumber(string.sub(val, 1, task - 1))
	local cnt= tonumber(string.sub(val,task+1,#val))
	
	local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
	DailyTaskLogic.recordDailyTaskFinishCnt(human, id, cnt, nil)
end

function d3.os(human, value)
	value = value and tonumber(value)
	
	if value == nil then
		local openSvrTime = CommonDB.getServerOpenTime()
		if openSvrTime == 0 then
			Broadcast.sendErr(human, "未设置开服时间")
		else
			local tb = os.date("%Y%m%d", openSvrTime)
			Broadcast.sendErr(human, "当前开服时间为" .. tb)
		end
	else
		local now = os.time()
		local temp = Util.getDayStartTime(now)
		temp = temp - (value - 1) * 24 * 3600
	
		CommonDB.setServerOpenTime(temp)
		
		local tb = os.date("%Y%m%d", temp)
		Broadcast.sendErr(human, "设置当前开服时间为" .. tb)
		
		--同步中心服开服时间
		--[[local msgRet = InnerMsg.lw.LW_OS
		msgRet.uuid = human.db._id
		msgRet.value = temp
		InnerMsg.sendMsg(0, msgRet)]]
	end	
end

function d3.createtime(human, value)
	value = value and tonumber(value)
	if not value then return end

	local now = os.time()
	local temp = Util.getDayStartTime(now)
	temp = temp - (value - 1) * 24 * 3600

	human.db.createTime = temp
	
	local tb = os.date("%Y%m%d", temp)
	Broadcast.sendErr(human, "设置当前创角时间为" .. tb)
end

--设置中心服开服时间
function d3.osw(human, value)
	value = value and tonumber(value)
	
	local msgRet = InnerMsg.lw.LW_OS
	msgRet.uuid = human.db._id
	msgRet.value = value
	InnerMsg.sendMsg(0, msgRet)
end

function d3.chengjiu(human,val)
	local param = Util.split(val, " ")
	local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
	ChengjiuLogic.onCallback(human,3,3,4)
end

function d3.jjczero()
	local JjcLogic = require("jjc.JjcLogic")
	JjcLogic.onZero()
end

function d3.jjcreset()
	JjcDB.resetJJCDB()
end

function d3.jpSet(human,val)
	local point = tonumber(val) 
    JjcDB.updatePlayerPoint(human.db._id, point, nil)
end

function d3.billboard(human)
	local BillboardDB = require("billboard.BillboardDB")
	BillboardDB.sort()
end

function d3.cldjs(human)
	human.db.jinbiExchange = nil
end

-- 激活钻石基金
function d3.fund(human, val)
    local type = tonumber(val) 

	local FundLogic = require("present.FundLogic")
	FundLogic.fundBuy(human, type)
end

-- 清空钻石基金购买和发放记录
function d3.clfund(human)
	human.db.fundFlag = nil
end

function d3.clbuy(human)
	human.db.buy = {}
end


--增加好战者计数  仅限竞技场和试炼
function d3.hzzadd(human, val)
	if val == nil then return end
	local pos = string.find(val, " ", 1, true)
	if pos == nil then return end
		
	local type = tonumber(string.sub(val, 1, pos - 1))
	local add= tonumber(string.sub(val,pos+1,#val))

	local MonthHzz = require("monthAct.MonthHzz")
	if type == MonthHzz.MONTHACT_HZZ_TYPE_1 then
		MonthHzz.addJjcFight(human, nil, add)
	elseif type == MonthHzz.MONTHACT_HZZ_TYPE_2 then
		MonthHzz.addTrialCnt(human, nil, add)
	end
end 

function d3.ydjiuguan(human, val)
	if val == nil then return end
	local pos = string.find(val, " ", 1, true)
	if pos == nil then return end

	local star = tonumber(string.sub(val, 1, pos - 1))
	local add= tonumber(string.sub(val,pos+1,#val))

	local MonthDrunkery = require("monthAct.MonthDrunkery")
	MonthDrunkery.addDrunkCntEx(human, star, add)
end

function d3.ydhcjx(human, val)
	if val == nil then return end
	local param = Util.split(val, " ")

	local type = tonumber(param[1])
	local heroId= tonumber(param[2])
	local add = tonumber(param[3])
	local MonthHecJuex = require("monthAct.MonthHecJuex")
	MonthHecJuex.addCntByGm(human,type,heroId, add)
end

function d3.clboss(human)
    local DB = require("common.DB")
	local LuaMongo = _G.lua_mongo  
	LuaMongo.remove(DB.db_boss)
end

function d3.clwish(human)
    human.db.wishCnt = nil
    human.db.wishActAlrCnt = nil
end

-- 设置高抽次数
function d3.hsum(human,val)
    human.db.summonCnt = val
end

function d3.clhsum(human)
    human.db.summonCnt = nil
    human.db.sumWinScoCnt = nil
end

function d3.unionquery(human,val)
    local name = val 
    local UnionLogic = require("union.UnionLogic")
    UnionLogic.createUnion(human,name)
end

function d3.uniondonate(human, value)
	local UnionDBLogic = require("union.UnionDBLogic")
    local union = UnionDBLogic.getUnion(human.db.unionUuid)
    if union == nil then
        return
    end

    union.donate = union.donate or {}
    union.donate[1] = value
    union.donate[2] = os.time()
    UnionDBLogic.updateUnionData(union)    
end

function d3.settower(human,val)
	local HuanjingTowerLogic = require("huanjingTower.HuanjingTowerLogic")
	HuanjingTowerLogic.setTowerByGm(human, val)
end

function d3.setbattle(human,val)
	local BattleLogic = require("battle.BattleLogic")
	local param = Util.split(val, " ")
	BattleLogic.setBattleByGm(human, param[1], param[2])
end

function d3.overmonth(human, val)
	val = math.max(tonumber(val) or 0, 10)
	human.db.actMonthEndTime = os.time() + val
end

function d3.zhuanpantime(human,val)
	local param = Util.split(val, " ")
	local ZhuanpanLogic = require("zhuanpan.ZhuanpanLogic")
	ZhuanpanLogic.zhuanpanByGm(human, tonumber(param[1]), tonumber(param[2]) )
end

function d3.signin(human,val)
    human.db.signInCnt = val
    for i =1,human.db.signInCnt do 
        human.db.signIn[i] = 1
    end
end

function d3.unionLv(human, val)
	val = tonumber(val)
	if not val then return end
    local UnionDBLogic = require("union.UnionDBLogic")
    local union = UnionDBLogic.getUnion(human.db.unionUuid)
    if not union then return end

    UnionDBLogic.upsetUnionData(union._id, "lv", val)
    UnionDBLogic.upsetUnionData(union._id, "exp", 0) 
end

function d3.unionexp(human, val)
	val = tonumber(val)
	if not val then return end
    local UnionDBLogic = require("union.UnionDBLogic")
    local UnionLogic = require("union.UnionLogic")
    local union = UnionDBLogic.getUnion(human.db.unionUuid)
    if not union then return end

    UnionLogic.addUnionExp(union, val) 
    UnionDBLogic.updateUnionData(union)
end

function d3.gws(human,val)
    local LoginSignLogic = require("loginSign.LoginSignLogic")
    for i = 1,val do
        LoginSignLogic.getReward(human)
    end
end

function d3.tsht(human)
    local AdminLogic = require("AdminLogic")
    local JsonInput = {}
    JsonInput.accountName = human.db.account
    JsonInput.roleName = human.db.name
    AdminLogic.adminFunction.getuserstatus(JsonInput)
end

function d3.bs(human,val)
    local param = Util.split(val, " ")
    local AdminLogic = require("AdminLogic")
    local JsonInput = {}
    JsonInput.account = human.db.account
    JsonInput.minute = param[1]
    JsonInput.reason = param[2]
    AdminLogic.adminFunction.banSay(JsonInput)
end

function d3.bl(human,val)
    local AdminLogic = require("AdminLogic")
    local JsonInput = {}
    JsonInput.accountName = human.db.account
    JsonInput.minute = val
    AdminLogic.adminFunction.banAccount(JsonInput)
end

function d3.bl2(human,val)
    local AdminLogic = require("AdminLogic")
    
	local param = Util.split(val, " ")

    local JsonInput = {}
    JsonInput.accountName = param[1]
    JsonInput.minute = tonumber(param[2])
    AdminLogic.adminFunction.banAccount(JsonInput)
end

function d3.cbl(human,val)
    local param = Util.split(val, " ")
    local AdminLogic = require("AdminLogic")
    local JsonInput = {}
    JsonInput.accountName = param[1]
    JsonInput.cancelBanSay = param[2]
    AdminLogic.adminFunction.cancelBanAccount(JsonInput)
end

function d3.bip(human,val)
    local param = Util.split(val, " ")
    local AdminLogic = require("AdminLogic")
    local JsonInput = {}
    JsonInput.IP = param[1]
    JsonInput.banTime = param[2]
    AdminLogic.adminFunction.banIp(JsonInput)
end


function d3.ursc(human)
    human.db.ectypeCnt = nil
    human.db.ectypeFreeTime = nil
end

function d3.sue(human,val)
	local newLevel = tonumber(val)
	if not newLevel then return end
	local UnionDBLogic = require("union.UnionDBLogic")
    local union = UnionDBLogic.getUnion(human.db.unionUuid)
    if not union then return end
    UnionDBLogic.updateBossLv(union,newLevel)
end

function d3.smtp(human,val)
    local param = Util.split(val, " ")
    local AdminLogic = require("AdminLogic")
    local JsonInput = {}
    JsonInput.recvType = 3
    JsonInput.title = param[1]
    JsonInput.content = param[2]
    JsonInput.fbAccount = param[3]
    JsonInput.senderName = "系统邮件"
    JsonInput.fbTime = os.time()
    JsonInput.fbContent = "反馈"
    JsonInput.startLv  =0
    JsonInput.endLv = 51
    JsonInput.lastLoginTime = os.time() - 60*60
    AdminLogic.adminFunction.mail(JsonInput)
end

function d3.clmilltime(human)
	human.db.unionTaskGetTime = nil
end

function d3.tstx(human,val)
    local RoleHeadLogic = require("role.RoleHeadLogic")
    RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_1, val)
end
function d3.tstxk(human,val)
    local RoleHeadLogic = require("role.RoleHeadLogic")
    RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_2, val)
end

function d3.buy(human,val)
	local AdminLogic = require("AdminLogic")
	local PlatformHandler = require("platform.Handler")
	local ApiLogic = require("platform.ApiLogic")
	local BuyExcel = require("excel.buy")
	local buyConf = BuyExcel.buy[val]
	if not buyConf then return end
	local shopConf = BuyExcel.topup[val]
	if shopConf and shopConf.buyID == val then
	
	else
		shopConf = nil
	end	
	
	local checkMoney = buyConf["CN"]
	
	local msg = {}
	msg.logintype = 2
	msg.openid = "oCD-L5RxAtfRe8-aiezdY-Ccla5U"
	msg.openkey = "VsBJf2DKah7QhzU5xuzLNA=="
	msg.yuanbao = shopConf and shopConf.cnt or buyConf.CN * 10
	msg.buyID = val
	msg.account = human.db.account
	msg.transaction_id = "transaction_id1"
	msg.pf = "android"
	msg.money = checkMoney
	ApiLogic.CHARGE_CODE_HIS = {}
	--PlatformHandler.CG_TENCENT_BUY(human,msg)
	AdminLogic.adminFunction.tencentPay(msg)
end


function d3.gol(human)
    local AdminLogic = require("AdminLogic")
    AdminLogic.adminFunction.getonlinelist()
end

function d3.htm(human,val)
    local AdminLogic = require("AdminLogic")
    local oJsonInput = {}
    oJsonInput.recvType = val			-- 邮件类型
	oJsonInput.title = "111"					-- 邮件标题
	oJsonInput.content = "222" 				-- 邮件内容
	oJsonInput.items = nil 					-- 道具列表 可以为空
	oJsonInput.accounts = nil	-- 指定玩家列表
    oJsonInput.startLv = 0
    oJsonInput.endLv = 200
    AdminLogic.adminFunction.mail(oJsonInput)
end

function d3.cluwar(human)
	require("union.UnionWarLogic").cleanUnionWar()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "cluwarmiddle"
    InnerMsg.sendMsg(0, msg) 
end

function d3.cluniontime(human)
	human.db.unionJoinTime = nil
end

function d3.cluwarmiddle()
	local LuaMongo = _G.lua_mongo
	local DB = require("common.DB")
	LuaMongo.remove(DB.db_union_war)
end

function d3.uwarstart()
	-- 需要周1、3、5使用
	local UnionWarLogic = require("union.UnionWarLogic")
	UnionWarLogic.cleanUnionWar()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "cluwarmiddle"
    InnerMsg.sendMsg(0, msg) 

	UnionWarLogic.onHour(0)
	UnionWarLogic.onHour(1)

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "uwarstartmiddle"
    InnerMsg.sendMsg(0, msg) 	
end

function d3.warMaxtest(human, value)
	require("union.UnionWarLogic").auto(value)
end

function d3.uwar(human,state)
	local msg = InnerMsg.lw.LW_GM
	msg.gm = "muwar "..state
    InnerMsg.sendMsg(0, msg) 

    if state == 1 then
		local UnionWarDBLogic = require("union.UnionWarDBLogic")
		UnionWarDBLogic.onHour(0, true)
    end
end

function d3.uwarshop(human, lv)
	require("union.UnionWarLogic").upWarShopLv(human, tonumber(lv))
end

function d3.uwarBuff(human)
	local msg = InnerMsg.lw.LW_GM
	msg.gm = "muwarBuff ".. human.db.unionUuid
    InnerMsg.sendMsg(0, msg) 

end

function d3.muwar(human,state)
	local UnionWarDBLogic = require("union.UnionWarDBLogic")
	if state == 1 then
		UnionWarDBLogic.onHour(0, true)
	elseif state == 2 then
		UnionWarDBLogic.onHour(2, true)
	elseif state == 3 then
		UnionWarDBLogic.onHour(20, true)
	end
end

function d3.muwarBuff(human, unionUuid)
	local UnionWarDBLogic = require("union.UnionWarDBLogic")
	UnionWarDBLogic.addBuffLvByGm(unionUuid)
end

function d3.uwartask(human, type)
	local UnionWarLogic = require("union.UnionWarLogic")
	UnionWarLogic.chegnjiuFinishGm(human, tonumber(type))
end

function d3.uwarstartmiddle()
	-- 匹配
	require("union.UnionWarMiddleLogic").matchUnionStart()
end

function d3.cljjc(human, val)
	val = tonumber(val)

	local msgRet = InnerMsg.lw.LW_CL_JJC
	msgRet.type = val
	InnerMsg.sendMsg(0, msgRet)
end

function d3.cluwar(human)
	require("union.UnionWarLogic").cleanUnionWar()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "cluwarmiddle"
    InnerMsg.sendMsg(0, msg) 
end

function d3.cluwarmiddle()
	local LuaMongo = _G.lua_mongo
	local DB = require("common.DB")
	LuaMongo.remove(DB.db_union_war)
end

function d3.uwarstart()
	-- 需要周1、3、5使用
	require("union.UnionWarLogic").onHour(0)
	require("union.UnionWarLogic").onHour(1)

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "uwarstartmiddle"
    InnerMsg.sendMsg(0, msg) 	
end

function d3.uwarstartmiddle()
	-- 匹配
	require("union.UnionWarMiddleLogic").matchUnionStart()
end


function d3.cxh(human)
    d3.tgod(human)
    d3.lv(human,200)
end


function d3.drop(human,val)
    local BattleLogic = require("battle.BattleLogic")
    local param = Util.split(val, " ")
    local cnt = tonumber(param[1])
    local battleID = tonumber(param[2])
    BattleLogic.clacItemCntGM(human,cnt,battleID)
end

function d3.uif(human,val)
    local jsonInput = {}
    jsonInput.unionName = val
     local AdminLogic = require("AdminLogic")
    AdminLogic.adminFunction.unionInfo(jsonInput)
end

function d3.ul(human,val)
    local jsonInput = {}
    jsonInput.start = tonumber(val)
     local AdminLogic = require("AdminLogic")
    AdminLogic.adminFunction.unionList(jsonInput)
end

function d3.monthgaochou(human, val)
    local MonthSummon = require("monthAct.MonthSummon")  
    MonthSummon.onSummon(human,  tonumber(val))
end

function d3.jhtq(human,val)
    local TequanLogic = require("qqTequan.TequanLogic")
    local param = Util.split(val, " ")
    local val1 = tonumber(param[1])
    local val2 = tonumber(param[2])
    local val3 = tonumber(param[3])
    local val4 = tonumber(param[4])
    TequanLogic.activateTequan(human,val1,val2,val3,val4)
end

function d3.unionTecLv(human,val)
    local UnionTecLogic = require("union.UnionTecLogic")
    UnionTecLogic.lvAllUpByGm(human)
end

function d3.auto(human,val)
    local param = Util.split(val, " ")
    local str = param[1]
    local val = param[2]
    if not str or not val then
    	return 
    end
    for i = 1, val do
    	require("union.UnionWarLogic").auto(str .. string.format("%02d", i))
    end    
end

function d3.lvAuto(human, val)
	val = val + 0
	local lvOld = human.db.lv
	local lvNew = val + 0
	human.db.lv = lvNew
end

function d3.cap(human)
	human.db.buyCapCnt = require("hero.HeroDefine").BUY_CAP_CNT
	require("hero.HeroLogic").sendHeroBagCap(human)
end

function d3.abscharge(human)
   AbsBillboardLogic.charge(human,1000)
end

function d3.jhpt(human)
    if human.pf_info.pf == "qqgame" then
        human.db.txHall = {} 
    elseif human.pf_info.pf == "qzone" or human.pf_info.pf == "website" then
        human.db.qqZone = {}
    end
end

function d3.addfuwen(human)
    d3.itemadd(human,"80001 10")
    d3.itemadd(human,"80002 10")
    d3.itemadd(human,"80003 10")
    d3.itemadd(human,"80004 10")
    d3.itemadd(human,"80005 10")
end

function d3.addSmelt(human,val)
    local num = tonumber(val)
    human.db.smeltVal = human.db.smeltVal or 0
    human.db.smeltVal = human.db.smeltVal + num
end

function d3.zdfw(human,val)
    local param = Util.split(val, " ")
    local fuwenID = tonumber(param[1])
    local attr1 = tonumber(param[2])
    local val1 = tonumber(param[3])
    local attr2 = tonumber(param[4])
    local val2 = tonumber(param[5])
    local skill1 = tonumber(param[6])
    local skill2 = tonumber(param[7])
    local FuwenGrid = require("fuwen.FuwenGrid")
    
    local FuwenExcel = require("excel.fuwen").skill
    if (skill1 ~= nil and FuwenExcel[skill1] == nil) or (skill2 ~= nil and FuwenExcel[skill2] == nil) then
        print("fuck! add fuwen skill is notreal")
        return
    end
    FuwenGrid.setFuwenSkillAttrByGM(human,fuwenID,attr1,val1,attr2,val2,skill1,skill2)
end

function d3.t2(human)
    local input = [[{"method":"OnTokenReturnForBuy","tencent_ret":{"ret":0,"token":"fC283BD0141D50418B68200972FBEE6A13102","url_params":"\/v1\/r\/1110099166\/mobile_goods_info?token_id=BC283BD0141D50418B68200972FBEE6A13102&transaction_id=BC283BD0141D50418B68200972FBEE6A13102&out_trade_no=&new_cpay=1&offer_type=0","transaction_id":"BC283BD0141D50418B68200972FBEE6A13102","attach":""},"callback_general_param":{"name":"0-15D9C743A68D57FF8B22CC944BAEAD8C","account":"0-a"}}]]
    local ret = require("AdminLogic").handleAdminRequest(_, input)
    print(ret)
end

function d3.t1(human)
    local input = {}
    input.token = "fC283BD0141D50418B68200972FBEE6A13102"
    input.payitem = "39*60*1"
    input.billno = os.time()
    local ret = require("platform.QQApi").onDeliverReturnForBuy(input)
    print(ret.ret,ret.msg)
end

function d3.skin(human,val)
    local tb = Util.split(val, " ")
    local id = tonumber(tb[1])
    local cnt = tonumber(tb[2])
    print(id,tb[1],tb[2])
    if not id then
        human.db.skinBag = {[0] = 999}
        return 
    end
    local SkinLogic = require("skin.SkinLogic")
    SkinLogic.addSkin(human,id,cnt)
end

function d3.ff(human,val)
    local FriendLogic = require("friend.FriendLogic")
    FriendLogic.addFriend(human, val)
end

function d3.chenghao(human,val)
	val = tonumber(val)
	if not val then
		return
	end
	local RoleHeadLogic = require("role.RoleHeadLogic")
	RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_4, val)
end

function d3.unionBillboard(human,val)
	val = tonumber(val)
	if not val then
		return
	end

	local BRoleLogic = require("billboard.BRoleLogic")
	BRoleLogic.queryUnionBillboard(human,val)
end

function d3.herotest(human, val)
    local tb = Util.split(val, " ")
    local combatType = 1

    local HeroExcel = require("excel.hero")
    local HeroLogic = require("hero.HeroLogic")
    local flags = {}
    human.db.combatHero = human.db.combatHero or {}
    human.db.combatHero[combatType] = {}
    for pos = 1, 6 do
        local id = tonumber(tb[pos])
        local index = nil
        if id and HeroExcel.hero[id] then
            for i = 1, human.db.heroBag[0] do
                local heroGrid = human.db.heroBag[i]
                if heroGrid and (heroGrid.id == id) and 
                    (not flags[i]) then
                    flags[i] = true
                    index = i
                    break
                end
            end
            if not index then
                index = HeroLogic.getEmptyIndex(human)
                d3.heroadd(human, id .. " 1")
            end
        end
        human.db.combatHero[combatType][pos] = index
    end
    local CombatHandler = require("combat.Handler")
    CombatHandler.CG_COMBAT_BEGIN(human,{type = combatType, param = ""})     
end

function d3.combat(human, monsterOutID)
	local CombatLogic = require("combat.CombatLogic")
	local param = {}
	param.defender, _, param.defRBase, param.defFormation = CombatLogic.getMonsterObjList(monsterOutID)
	require("combat.CombatLogic").combatBegin(human, 1001, param, 1)
end


function d3.combat2(human,id)
	local id = tonumber(id)
	local combatConf = require("excel.combat").gm[id]
	if not combatConf then
		assert()
	end
	local HeroGrid = require("hero.HeroGrid")
	local HeroLogic = require("hero.HeroLogic")
	local CombatLogic = require("combat.CombatLogic")

	local fakeman1 = {}
	local account1 = "combatFakeman1"
	local fakeman2 = {}
	local account2 = "combatFakeman2"
	local RoleDBLogic = require("role.RoleDBLogic")
	local fakemanDB1 = RoleDBLogic.getDbByAccount(account1)
	if not fakemanDB1 then
		fakemanDB1 = RoleDBLogic.createDefaultRole(account1)
	end
	fakeman1.db = fakemanDB1
	local fakemanDB2 = RoleDBLogic.getDbByAccount(account2)
	if not fakemanDB2 then
		fakemanDB2 = RoleDBLogic.createDefaultRole(account2)
	end
	fakeman2.db = fakemanDB2	

	local attacker = {}
	local defender = {}
	for i = 1, fakeman1.db.heroBag[0] do
		HeroLogic.delHeroByIndex(fakeman1, i, "gm")
	end

	for i = 1, fakeman2.db.heroBag[0] do
		HeroLogic.delHeroByIndex(fakeman2, i, "gm")
	end

    for k,v in ipairs(combatConf.atk) do
        local heroGrid = HeroGrid.createHeroGrid(v[1], v[4])
        heroGrid.lv = math.max(heroGrid.lv, v[3] or 0)

        heroGrid.quality = HeroGrid.getMaxQuality(heroGrid.star)
        local heroIndex = HeroLogic.addHeroByGrid(fakeman1, heroGrid, "gm")
        attacker[v[2]] = CombatLogic.createHumanObj(fakeman1, heroGrid.uuid)
    end

    for k,v in ipairs(combatConf.def) do
        local heroGrid = HeroGrid.createHeroGrid(v[1], v[4])
        heroGrid.lv = math.max(heroGrid.lv, v[3] or 0)
        heroGrid.quality = HeroGrid.getMaxQuality(heroGrid.star)
        local heroIndex = HeroLogic.addHeroByGrid(fakeman2, heroGrid, "gm")
        defender[v[2]] = CombatLogic.createHumanObj(fakeman2, heroGrid.uuid)
    end

	local atkPet = nil
	local defPet = nil
	CombatLogic.combatGm(human,1001,combatConf.atkF, attacker,combatConf.defF,defender,atkPet,defPet,{{101,1},{102,2},{103,3},{104,4}})
end

function d3.combatEnd(human)
    local CombatLogic = require("combat.CombatLogic")
    for i = CombatDefine.COMBAT_TYPE1,  CombatDefine.COMBAT_TYPE_MAX do
	   CombatLogic.clientFinish(human, i)
    end
end

-- it道具添加（支持中文）
function d3.it(human,value)
    local tb = Util.split(value," ")
    local id = tonumber(tb[1])
    local cnt = tonumber(tb[2]) 

    if id == nil then id = tb[1] end
    if type(id) == "string" then
        id = ItemDefine.getIdByName(id)
    end

    if type(id) ~= "number" then 
        print("err id", tb[1], id, type(id))
        return 
    end
    cnt = cnt or 1000
    BagLogic.addItem(human, id, cnt, "gm")
end

-- 清除酒馆
function d3.clbartask(human)
    human.db.bar = nil
end

function d3.updaily(human)
    ObjHuman.updateDaily(human, true)
end

function d3.clguide(human)
	human.db.noguide = nil
    human.db.guide = nil
    human.guideLoginStep = nil
    require("guide.GuideLogic").refreshGuide(human)
    require("guide.GuideLogic").sendStep(human)
end

function d3.nextguide(human)
    local GuideExcel = require("excel.guide")
    local GuideLogic = require("guide.GuideLogic")
    local id = GuideLogic.getGuideID(human) 
    local nextstep = GuideLogic.getGuideStep(human) + 1
    local cf = id and GuideExcel.main_guide[id]
    local sid = cf and cf.guideList[nextstep]
    GuideLogic.stepFinish(human, sid)
end

function d3.guide(human, id)
	human.db.noguide = nil
	human.guideLoginStep = nil
	local GuideLogic = require("guide.GuideLogic")
	GuideLogic.setGuide(human, id, GuideLogic.STATE_DOING)
	GuideLogic.sendStep(human)
end

-- 跳过指引
function d3.noguide(human)
	human.db.noguide = true 
	human.guideLoginStep = nil
	require("guide.GuideLogic").sendStep(human, true)
end

function d3.creatunion(human,value)
	d3.lv(human,30)
	d3.viplv(human,2)
	d3.zuanshi(human, 1000)
	local UnionLogic = require("union.UnionLogic")
	UnionLogic.createUnion(human, value)
end

function d3.cldraw(human, value)
	human.db.drawCard = {jifen=tonumber(value),list={[1]={},[2]={},[3]={},[4]={}}}
end

function d3.cldrill(human)
	require("drill.DrillDB").reset()
end

function d3.zhengdian(_, hour)
	hour = tonumber(hour) or 0
	require("core.Timer").doZhengDian(hour)
end

function d3.tgod(human)
	local HeroLogic = require("hero.HeroLogic")
	local HeroGrid = require("hero.HeroGrid")
	local HeroExcel = require("excel.hero")
	for heroID, config in pairs(HeroExcel.tujian) do
		local heroConfig = HeroExcel.hero[heroID]
		if heroConfig then
			local star = heroConfig.star
			local maxQuality = HeroGrid.getMaxQuality(star)
			local maxLv = HeroGrid.getMaxLv(star,maxQuality)
			d3.heroadd(human, heroID .. " 1".." "..maxLv.." "..maxQuality)
		end
	end
end

function d3.tujian(human, camp)
	local HeroExcel = require("excel.hero")
	camp = tonumber(camp)
	for heroID, config in pairs(HeroExcel.tujian) do
		if config.camp == camp or not camp then
			d3.heroadd(human, heroID .. " 1")
		end
	end
end

function d3.cljingcailibao(human)
	human.db.dailyLeiChong = nil
	human.db.leichongHaoli = nil
	human.db.topupAcountDaily = nil
	human.db.topupAccountMonth = nil
end

function d3.qijifen(human,value)
	human.db.sevenDayGoal = human.db.sevenDayGoal or {}
	human.db.sevenDayGoal[9] = human.db.sevenDayGoal[8] or {}
	human.db.sevenDayGoal[9].jifen = value
end

function d3.clqirimubiao(human)
	human.db.sevenDayGoal = nil
end

function d3.throne(human, str)
	if str == nil then return end
	local tb = Util.split(str, " ")
	local id = tonumber(tb[1])
	local lv = tonumber(tb[2])
	if not id or not lv then return end
	local TheStarsDBLogic = require("theStars.TheStarsDBLogic")
	local list = TheStarsDBLogic.getThroneList() 
	local throne = list[id]
	if not throne then return end
	throne.lv = lv
	TheStarsDBLogic.updateThroneDB(throne)
end

function d3.cltsts(human)
    human.db.throneTime = human.db.throneTime - 14400
end

-- 删号
function d3.delaccount(human, account)
	if not account then return end
	local target = ObjHuman.onlineAccount[account]
	if target then
		return Broadcast.sendErr(human, "can't delete online")
	end

	local RoleDBLogic = require("role.RoleDBLogic")
	if not RoleDBLogic.getDbByAccount(account) then
		return Broadcast.sendErr(human, "account not exist")
	end

	local LuaMongo = _G.lua_mongo
	local DB = require("common.DB")
	local query = {}
	query.account = tostring(account)
	LuaMongo.remove(DB.db_char, query)
	require("core.RoleDel").roleDel()

	Broadcast.sendErr(human, "delete ok, account = " .. account)
end

-- 临时开指引
function d3.openguide()
	require("Config").HAS_GUIDE = true
end

function d3.closeguide()
	require("Config").HAS_GUIDE = false
end

-- 自动上阵
function d3.autoteam(human, combatType)
	if tonumber(combatType) == nil then return end
	local CombatPosLogic = require("combat.CombatPosLogic")
	local str = ""
	for i = 1, 6 do
		if not human.db.heroBag[i] then
			local heroID = require("hero.HeroDefine").getRandHeroByIsElite()
			local lv = math.random(50, 100)
		 	d3.heroadd(human, heroID .. " 1 " .. lv)
		end
		str = str .. i 
		if i % 6 == 0 then str = str .. "," 
		else str = str .. "|" end
	end
	CombatPosLogic.updatePos(human,combatType,str)	
end

function d3.fcmstate(human, state)
	human.fcmState = state
	require("fcm.FcmLogic").checkFcm(human)
end

function d3.oltimeday(human, time)
	time = tonumber(time)
	if not time then return end

	human.db.onlineTimeDay = time
	require("fcm.FcmLogic").checkFcm(human)
end

function d3.dis(human, code)
	ObjHuman.doDisconnect(human, code)
end

function d3.project(human)
	local Lang = require("common.Lang")
	Broadcast.sendErr(human, Lang.TEST_MULTY_PROJECT)
end

function d3.clextrabattle(human)
	human.db.battleExtraReward = nil
end

function d3.clsharepf(human)
	human.db.plShare = nil
end

function d3.clshareboard()
	require("present.SharePlatformLogic").resetBoard()
end

function d3.sharepfall(human, cnt)
	cnt = tonumber(cnt) or 15
	for i = 1, cnt do
		require("present.SharePlatformLogic").onShareOk(human.db.account, "account" .. i, true)
	end
end

function d3.sharepf(human, accountB)
	require("present.SharePlatformLogic").onShareOk(human.db.account, accountB)
end

function d3.sharepf2(human, accountA)
	require("present.SharePlatformLogic").onShareOk(accountA, human.db.account)
end

function d3.sharegroup(human)
	require("present.SharePlatformLogic").groupShareOK(human)
end

function d3.shareinvate(human)
	require("present.SharePlatformLogic").shareInvate(human)
end

function d3.cltuisonglibao(human)
	human.db.tuiSongLiBao = nil
end

function d3.clmoshou(human)
	human.db.moshou = nil
end

function d3.moshoulv(human,lv)
	local MoShouExcel = require("excel.moshou")
	if not lv or lv <= 0 or lv > #MoShouExcel.moshouLv then return end

	local MoshouLogic = require("moshou.MoshouLogic")
	local isAllActive = MoshouLogic.isAllMoshouActive(human)
	if not isAllActive then
		local MoShouExcel = require("excel.moshou")
		local config = MoShouExcel.moshouActive
		for i=1,#config do
			local activeTask = config[i].activeTask
			for j=1,#activeTask do
				human.db.moshou = human.db.moshou or {}
				human.db.moshou[i] = human.db.moshou[i] or {}
				human.db.moshou[i].taskGet = human.db.moshou[i].taskGet or {}
				human.db.moshou[i].taskGet[j] = j
			end
		end
	end

	human.db.moshou.lv = lv
	human.db.moshou.lvExp = MoShouExcel.moshouLv[lv].needJinDu
	human.db.moshou.skillLV = human.db.moshou.skillLV or 1
	MoshouLogic.calMoshouHero(human)
end

function d3.activemoshou(human,moshouID)
	if not moshouID or moshouID <= 0 then return end
	local MoShouExcel = require("excel.moshou")
	local config = MoShouExcel.moshouActive

	for i=1,moshouID do
		local activeTask = config[i].activeTask
		for j=1,#activeTask do
			human.db.moshou = human.db.moshou or {}
			human.db.moshou[i] = human.db.moshou[i] or {}
			human.db.moshou[i].taskGet = human.db.moshou[i].taskGet or {}
			human.db.moshou[i].taskGet[j] = j
		end
	end

	if moshouID == #config then
		d3.moshoulv(human,1)
	end

	local MoshouLogic = require("moshou.MoshouLogic")
	MoshouLogic.calMoshouHero(human)
end

function d3.moshouupneed(human)
	local MoShouExcel = require("excel.moshou")
	local lvUpNeed1 = MoShouExcel.moshouLv[2].lvUpNeed[1][1]
	local lvUpNeed2 = MoShouExcel.moshouLv[2].lvUpNeed[2][1] 
	d3.it(human,lvUpNeed1)
	d3.it(human,lvUpNeed2)

	local jingLianUp1 = MoShouExcel.moshouJingLian[2].lvUpNeed[1][1]
	local jingLianUp2 = MoShouExcel.moshouJingLian[2].lvUpNeed[2][1]
	d3.it(human,jingLianUp1)
	d3.it(human,jingLianUp2)

	local zhuhunUp = MoShouExcel.moshouZhuHun[1].costID
	d3.it(human,zhuhunUp)

	local skillup1 = MoShouExcel.moshouSkillLv[2].upCost[1][1]
	local skillup2 = MoShouExcel.moshouSkillLv[2].upCost[2][1]
	d3.it(human,skillup1)
	d3.it(human,skillup2)
end

function d3.moshoujinglian(human,val)
	if not human.db.moshou then return end
	human.db.moshou.jingLian = val
end

function d3.clolinereward(human)
	require("onlineReward.OnlineRewardLogic").initDB(human)
end

function d3.clonlinerewardget(human)
	human.db.onlineReward.getId = 0
end

function d3.bartaskfinish(human)
	local BarTaskLogic = require("bar.BarTaskLogic")
	BarTaskLogic.initDB(human)
	for taskID, taskData in pairs(human.db.bar) do 
		if type(taskID) == "number" then
			human.db.bar[taskID].ts = 0
		end
	end
end

function d3.test(human)
	local str = Util.format("aa{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
	Broadcast.sendErr(human, str)
	print(str)
end

function d3.clbattle()
    local DB = require("common.DB")
	local LuaMongo = _G.lua_mongo 
	LuaMongo.remove(DB.db_battle_shark)
end

-- 设置天梯争霸状态 
function d3.jjcladderstart()
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	local JjcLadderExcel = require("excel.jjcLadder")
	local JjcLadderDefine = JjcLadderExcel.define[1]

	JjcLadderLogic.onActReady()
	local week = Util.getWeekDay(os.time()) - 1
	if week == 0 then
		week = 7
	end
	JjcLadderLogic.TEMP_TIMER_STAMPTIME = nil 
	JjcLadderDefine.startTime.week = week
	JjcLadderDefine.endTime.week = week + 1
	JjcLadderLogic.onTimer()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "jjcladderstartmiddle"
    InnerMsg.sendMsg(0, msg) 	
end

function d3.jjcladderstartmiddle()
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	local JjcLadderExcel = require("excel.jjcLadder")
	local JjcLadderDefine = JjcLadderExcel.define[1]

	JjcLadderLogic.onActReady()
	local week = Util.getWeekDay(os.time()) - 1
	if week == 0 then
		week = 7
	end
	JjcLadderLogic.TEMP_TIMER_STAMPTIME = nil 
	JjcLadderDefine.startTime.week = week
	JjcLadderDefine.endTime.week = week + 1
	JjcLadderLogic.onTimer()
end

-- 天梯争霸报错
function d3.jjcladdersign(human)
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
		local commonDB = JjcLadderLogic.getCommonDB()
		commonDB.uuid2sign = commonDB.uuid2sign or {}
		commonDB.uuid2sign[human.db._id] = os.time()
		JjcLadderLogic.saveCommonDB(commonDB)
		JjcLadderLogic.onUpdatePos(human)
	end
end

-- 天梯争霸结束回调
function d3.jjcladderend()
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	JjcLadderLogic.onActEnd()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "jjcladderendmiddle"
    InnerMsg.sendMsg(0, msg) 
end

function d3.jjcladderendmiddle()
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	JjcLadderLogic.onActEnd()
end


-- 重置天梯争霸活动信息
function d3.cljjcladder()
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	JjcLadderLogic.saveCommonDB()
	JjcLadderLogic.onActEnd()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "cljjcladdermiddle"
    InnerMsg.sendMsg(0, msg) 
end

function d3.cljjcladdermiddle()
	local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
	JjcLadderLogic.saveCommonDB()
	JjcLadderLogic.onActEnd()
end

function d3.clcombat(human,val)
	human.db.combatHero[val] = nil
end

function d3.clshop(human,val)
	human.db.shop[val] = nil
end

-- 重置日常礼包
function d3.clrichanglibao(human)
	human.db.richangLibao = nil
end

-- 重置荣耀峡谷（龙族战场）
function d3.clvalley()
	local ValleyLogic = require("valley.ValleyLogic")
	ValleyLogic.cleanDB()

	local msg = InnerMsg.lw.LW_GM
	msg.gm = "clvalleymiddle"
    InnerMsg.sendMsg(0, msg) 
end

function d3.clvalleymiddle()
	local ValleyLogic = require("valley.ValleyLogic")
	ValleyLogic.cleanDB()
end

-- 重置荣耀峡谷（龙族战场）成就
function d3.clvalleytask(human)
	human.db.valleyTask = nil
end

-- 增加荣耀峡谷（龙族战场）个人成就值
function d3.valleytask1(human, value)
	require("valley.ValleyTask").updateValue(human.db._id, value, 0)
end

-- 增加荣耀峡谷（龙族战场）团队成就值
function d3.valleytask2(human, value)
	require("valley.ValleyTask").updateValue(human.db._id, 0, value)
end

function d3.touchLv(human,val)
    local liveness = val + 0
    require("union.UnionLivenessLogic").touchLv(human, liveness)
end

function d3.clyj()
	local DB = require("common.DB")
	local LuaMongo = _G.lua_mongo  
    LuaMongo.remove(DB.db_yjTreasure)
    local YjTreasureDBLogic = require("yjTreasure.YjTreasureDB")
    YjTreasureDBLogic.YJ_TREASURE_DB = {}
end

function d3.yjlayer(human,val)
	local YjTreasureLogic = require("yjTreasure.YjTreasureLogic")
	local YjTreasureDBLogic = require("yjTreasure.YjTreasureDB")
	local YjTreasureExcel = require("excel.yjTreasure")
	local Lang = require("common.Lang")
	local layerData = YjTreasureDBLogic.getLayerDataByUuid(human.db._id)
	local layCnf = YjTreasureExcel.layer
	local nowLayer = layerData.layer

	if val > #layCnf then return Broadcast.sendErr(human,Lang.YJTREASURE_LAYER_ERR) end
	local roleData = YjTreasureDBLogic.getRoleDataByUuid(human.db._id)
	local layerMax = roleData.layerMax or 0
	if layerMax < val then
		roleData.layerMax = val
		YjTreasureDBLogic.updateRoleDataByUuid(roleData,human.db._id)
	end

	YjTreasureDBLogic.creatLayerDataByUuid(human.db._id,val)
	
	YjTreasureLogic.CG_YJTREASURE_QUERY(human)
end


function d3.towerKing(human)
	local HuanJingTowerLogic = require("huanjingTower.HuanjingTowerLogic")
	HuanJingTowerLogic.unlockTowerKing(human)
end

-- 设置炼狱最大通关层
function d3.lianyu(human, value)
	value = tonumber(value)
	if not value then return end
	if value < 0 then return end
	local LianyuExcel = require("excel.lianyu")
	if value > 0 and not LianyuExcel.gamelevel[value] then
		return
	end
	LianyuLogic = require("lianyu.LianyuLogic")
	LianyuLogic.setMaxLv(human, value)

end

function d3.cllianyu(human)
	human.db.lianyu = nil
end

function d3.cllianyuhelp()
	local CommonDB = require("common.CommonDB")
	CommonDB.updateValue(CommonDB.KEY_LIANYU, nil)
end

-- 清除特权商店相关信息
function cltequanshop(human)
	human.db.tequanShop = nil
	human.db.tequanMopup = nil
	if human.db.bar then
		human.db.bar.vip = nil
		human.db.bar.surVip = nil
	end
end

function d3.addbts(human)
    local now = os.time()
    require("battle.BattleLogic").initBattleOut(human, now)
    human.db.battleOut.expTs1 = now	- 7200	   
    human.db.battleOut.itemTs1 = now - 7200	   
    human.db.battleOut.expTs2 = now	- 7200	   
    human.db.battleOut.itemTs2 = now - 7200	   
end

function d3.addbsd(human)
    human.db.mopupDoCnt =  0
    human.db.mopupFreeCnt = 0
end

function d3.addYjYj(human,type)
    local YjTreasureLogic = require("yjTreasure.YjTreasureLogic")
    YjTreasureLogic.yjYaoJiAdd(human,10,type)
end

function d3.ajfc(human,val)
    
    for i = 1,val do
	human.db.jjcDailyFight = (human.db.jjcDailyFight or 0) + 1	
	require("jjc.JjcDB").updateTotalFight(human.db._id)
    end
end

function d3.godWartest(human)
	local JjcGodWarLogic = require("jjcGodWar.JjcGodWarLogic")
	JjcGodWarLogic.jjcGodWarFinishGm(human)
end

function d3.closeMiddle(human)
	MiddleConnect.stopConnectMiddle()
end

function d3.resetMiddle(human)
	MiddleConnect.autoResetMiddle()
end

function d3.loginSign(human, value)
	value = tonumber(value)
	local LoginSignLogic = require("loginSign.LoginSignLogic")
	LoginSignLogic.updateGM(human, value)
end

function d3.yuanjun(human, value)
   human.db.combatBackup = 1
end

function d3.buyItem(human, value)

   require("shop.ShopLogic").buy(human, 9, 405007, 1, 1)
end

function d3.test(human)
	require("absAct.MangHeLogic").draw(human, 1)
end

function d3.zbsj(human,val)
    if human.db.zhanbu == nil then
        return
    end
    human.db.zhanbu.money = val
end

function d3.tjm(human)
    require("present.RelationGiftLogic").relationBind(human, "017772140001")
end

function d3.qcjz(human)
    human.db.unionDonate = nil
end

function d3.unionCopyLv(human, val)
	require("union.UnionEctypeLogic").toUnionBossLvGm(human, tonumber(val))
end

function d3.fff(human)
    human.db.leaveUnionLimit = nil
end

function d3.hgw(args)
    require("absAct.HeroGrowUp").actLoop()
end

function d3.hgwlv(human,val)
    require("absAct.HeroGrowUp").addJifen(human,tonumber(val))
end

function d3.paoma(human, val)
    require("chat.ChatPaoMaLogic").notice(val)
end

-- 设置合服时间
function d3.merge(human,value)
    value = value and tonumber(value)
	
	if value == nil then
		local mergeSvrTime = CommonDB.getMergeServerTime()
		if mergeSvrTime == 0 then 
			Broadcast.sendErr(human, "未设置合服时间")
		else
			local tb = os.date("%Y%m%d", mergeSvrTime)
			Broadcast.sendErr(human, "当前合服时间为" .. tb)
		end
	else
		local now = os.time()
		local temp = Util.getDayStartTime(now)
		temp = temp - (value - 1) * 24 * 3600
	
		CommonDB.setMergeTime(temp)
		
		local tb = os.date("%Y%m%d", temp)
		Broadcast.sendErr(human, "设置当前合服时间为" .. tb)
		
		--同步中心服开服时间
		--[[local msgRet = InnerMsg.lw.LW_OS
		msgRet.uuid = human.db._id
		msgRet.value = temp
		InnerMsg.sendMsg(0, msgRet)]]
	end	
end
